package device_ctr;
import com.ouken.activity.*;
import java.util.*;

import db_ctr.db_RW;
import device_ctr.Wifi_ctronller;

import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.util.Log;
public class Device_ctr{
	private ArrayList<Dev_para> Device_list;
	private Context context;
	private int dev_num;
	public Device_ctr(Context context) {
		super();
		Device_list=new ArrayList<Dev_para>();
		this.context = context;
	}
	public ArrayList<Dev_para> getDevice_list() {
		return Device_list;
	}
	private void add_device(
		String ssid,
		int device_type,
		String device_name,
		String device_des,
		int single_qiangdu,
		int device_status){
		
		Dev_para device_temp=new Dev_para(" "," ",ssid,device_type,device_name,device_des,single_qiangdu,device_status);
		this.Device_list.add(device_temp);
		dev_num=dev_num+1;
	}
	public String Refresh(){
		Wifi_ctronller wifi_con =new Wifi_ctronller(this.context);
		db_RW kk=new db_RW(context);
		//kk.delete_device("a");
		this.Device_list=kk.get_list();
		if (wifi_con.get_wifi_status()==true){
			wifi_con.startScan();
			List<ScanResult> aa = wifi_con.getWifiList();
			String wifi_list="";
			for(int i=0;i<aa.size();i++){
				int single_level=0;
				if (aa.get(i).level>-90) single_level=5;
				if (aa.get(i).level>-80) single_level=4;
				if (aa.get(i).level>-70) single_level=3;
				if (aa.get(i).level>-60) single_level=2;
				if (aa.get(i).level>-50) single_level=1;
				
				for(int j=0;j<this.Device_list.size();j++){
					if (this.Device_list.get(j).ssid.equals(aa.get(i).SSID)){
						//this.Device_list.get(j).signal_qiangdu=single_level;
						Dev_para Dev_temp=new Dev_para (this.Device_list.get(j).BSSID,this.Device_list.get(j).ssid,this.Device_list.get(j).identify,this.Device_list.get(j).device_type,this.Device_list.get(j).device_name,this.Device_list.get(j).device_des,this.Device_list.get(j).signal_qiangdu,this.Device_list.get(j).device_status);
						Dev_temp.signal_qiangdu=single_level;
						this.Device_list.set(j, Dev_temp);
					}
				}
			}
			for(int j=0;j<this.Device_list.size();j++){
				if (this.Device_list.get(j).signal_qiangdu==0){
					if (wifi_list==""){
						wifi_list=this.Device_list.get(j).ssid + "," + this.Device_list.get(j).device_des + "," + String.valueOf(this.Device_list.get(j).signal_qiangdu) + ",0" +"," + this.Device_list.get(j).identify;
					}
					else{
						wifi_list=wifi_list+","+this.Device_list.get(j).ssid + "," + this.Device_list.get(j).device_des + "," + String.valueOf(this.Device_list.get(j).signal_qiangdu)+ ",0"+"," + this.Device_list.get(j).identify;
					}
				}
				else{
					if (wifi_list==""){
						wifi_list=this.Device_list.get(j).ssid + "," + this.Device_list.get(j).device_des + "," + String.valueOf(this.Device_list.get(j).signal_qiangdu) + ",1"+"," + this.Device_list.get(j).identify;
					}
					else{
						wifi_list=wifi_list+","+this.Device_list.get(j).ssid + "," + this.Device_list.get(j).device_des + "," + String.valueOf(this.Device_list.get(j).signal_qiangdu)+ ",1"+"," + this.Device_list.get(j).identify;
					}
				}
			}
			return wifi_list;
		}
		else{
			return "";
		}
	}
	public String get_all_net(){
		Wifi_ctronller wifi_con =new Wifi_ctronller(this.context);
		if (wifi_con.get_wifi_status()==true){
			wifi_con.startScan();
			List<ScanResult> aa = wifi_con.getWifiList();
			String wifi_list="";
			db_RW kk=new db_RW(context);
			
			for(int i=0;i<aa.size();i++){
				int single_level=0;
				if (aa.get(i).level<-90) single_level=5;
				if (aa.get(i).level<-80) single_level=4;
				if (aa.get(i).level<-70) single_level=3;
				if (aa.get(i).level<-60) single_level=2;
				if (aa.get(i).level<-50) single_level=1;
				
				if (aa.get(i).SSID.startsWith("QZ")&&(kk.If_in_DB(aa.get(i).SSID)==false)){
					if (wifi_list.length()==0){
						wifi_list=aa.get(i).SSID + ",0000," + String.valueOf(single_level);
					}
					else{
						wifi_list=wifi_list+"," + aa.get(i).SSID + ",0000," + String.valueOf(single_level);
					}
				}
				
			}
			return wifi_list;
		}
		else{
			return "";
		}
	}
    public boolean  AddWifiInfo(String SSID, String Password, int Type)   
    {   
          WifiConfiguration config = new WifiConfiguration();     
           config.allowedAuthAlgorithms.clear();   
           config.allowedGroupCiphers.clear();   
           config.allowedKeyManagement.clear();   
           config.allowedPairwiseCiphers.clear();   
           config.allowedProtocols.clear();   
           config.SSID = "\"" + SSID + "\"";     
            
          Wifi_ctronller wifi_con =new Wifi_ctronller(this.context);
          
          int tempID = this.check_wifi_ID(SSID);             
          if(tempID >0) {    
        	  wifi_con.disableNetWordLick(tempID); 
        	  //wifi_con.EnableNetWordLick(tempID);
          }  
          if(Type == 1) //WIFICIPHER_NOPASS  
          {   
               config.wepKeys[0] = "";   
               config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);   
               config.wepTxKeyIndex = 0;   
          }   
          if(Type == 2) //WIFICIPHER_WEP  
          {
              config.hiddenSSID = false;  
              config.wepKeys[0]= "\""+Password+"\"";   
              config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);   
              config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);   
              config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);   
              config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);   
              config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);   
              config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);   
              config.wepTxKeyIndex = 0;   
          }
          if(Type == 3) //WIFICIPHER_WPA  
          {
	          config.preSharedKey = "\""+Password+"\"";   
	          config.hiddenSSID = false;     
	          config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);     
	          config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);                           
	          config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);                           
	          config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);                      
	          //config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);    
	          config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);  
	          config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);  
	          config.status = WifiConfiguration.Status.ENABLED;     
          }
          return wifi_con.addNetWordLink(config); 
    }   
	
	private int check_wifi_ID(String SSID){
		Wifi_ctronller wifi_con =new Wifi_ctronller(this.context);
		wifi_con.startScan();
		List<WifiConfiguration> Wifi_Config_list = wifi_con.getWifiConfigList();
		for(int i=0;i<Wifi_Config_list.size();i++){
			if(Wifi_Config_list.get(i).SSID.equals('"' + SSID +'"')){
				return Wifi_Config_list.get(i).networkId;
			}
		}
		return -1;
	}
	public int Connect_wifi(String SSID){
		Wifi_ctronller wifi_con =new Wifi_ctronller(this.context);
		wifi_con.startScan();
		List<WifiConfiguration> Wifi_Config_list = wifi_con.getWifiConfigList();
		for(int i=0;i<Wifi_Config_list.size();i++){
			//Log.e("Duan", Wifi_Config_list.get(i).toString());
			if(Wifi_Config_list.get(i).SSID.equals('"' + SSID +'"')){
				if (wifi_con.EnableNetWordLick(Wifi_Config_list.get(i).networkId)==true)
					return R.string.wifi_Ok;
				else{
					return R.string.wifi_error;
				}
			}
		}
		AddWifiInfo(SSID,"12345678",3);
		wifi_con.startScan();
		List<WifiConfiguration> Wifi_Config_list_new = wifi_con.getWifiConfigList();
		for(int i=0;i<Wifi_Config_list_new.size();i++){
			//Log.e("Duan", Wifi_Config_list.get(i).toString());
			if(Wifi_Config_list_new.get(i).SSID.equals('"' + SSID +'"')){
				if (wifi_con.EnableNetWordLick(Wifi_Config_list_new.get(i).networkId)==true)
					return R.string.wifi_Ok;
				else{
					return R.string.wifi_error;
				}
			}
		}
		return R.string.wifi_error;
	}
}